import time

# from math import pi
from GroverEq import GroverEq
from BoolEquations import BoolEquations
# from qiskit import Aer, execute, QuantumRegister, ClassicalRegister, QuantumCircuit


def main():
    time_start = time.time()
    num_variable = 20
    # int(input("请输入变元数量："))
    print("运行规模: {:d}变元".format(num_variable))
    # 类对象
    equations = BoolEquations(num_variable)
    print("正在生成方程组及经典破解中...（请耐心等待）")
    # 运行算法，返回具有唯一解的方程组
    equations.run()

    # 打印方程组
    equations.print_equations()
    print("正在经典算法求解中...")
    print("经典算法：")
    # 打印结果
    print(equations.result_set)
    time_end = time.time()
    print('经典算法运行时间:%fs' % (time_end - time_start))

    # 方程分组
    eq_per_group = equations.split_integer()

    print("正在量子算法求解中...")
    time_start = time.time()
    grover = GroverEq(num_variable, equations.eq_set, equations.eq_value, eq_per_group)

    grover.construct(cycles=3)
    answer = grover.run(shots=1000000)
    
    print("量子算法：")
    # 概率最大的答案
    print(sorted(answer.items(),key=lambda x:x[1],reverse=True)[0])
    #print(answer)
    print("量子电路深度为：%d" % grover.circuit.depth())
    time_end = time.time()
    print('量子电路运行时间:%fs' % (time_end - time_start))
    # 需画图取消下行代码注释
    # grover.draw("mpl")

if __name__ == '__main__':
    main()
    
